package com.jilujia.domain.blog;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.jilujia.domain.blog.repository.BlogCommentRepository;
import com.jilujia.domain.blog.repository.BlogRepository;
import com.jilujia.framework.domain.AbstractPoEntityManager;

@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class BlogCommentManager extends AbstractPoEntityManager<Long, BlogComment, BlogCommentRepository> {

	@Autowired
	private BlogRepository blogRepository;

	@Transactional
	public BlogComment saveBlogComment(BlogComment blogCommentVo, Long userId) {
		Blog blog = blogRepository.findOne(blogCommentVo.getBlog().getId());
		blogCommentVo.setBlog(blog);

		// save comment
		BlogComment blogCommentPo = new BlogComment();
		blogCommentPo.setCreatedDate(new Date(System.currentTimeMillis()));
		blogCommentPo.setUserId(userId);
		blogCommentPo.processVo(blogCommentVo);

		// 之前所有的动作都是为了生成完整的PO
		this.setInstance(blogCommentPo);
		this.save();

		return blogCommentPo;
	}
}
